package com.dmp.storm.bolt.db;

import com.dmp.storm.bolt.BaseMapBolt;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


public abstract class InsertDBBolt extends BaseMapBolt {
    /**
	 * 
	 */
	private static final long serialVersionUID = 5474822546178269100L;
	protected String sql;
    protected Connection connection;
    private String path;

    public void setSql(String sql) {
        this.sql = sql;
    }

    public void setPath(String path) {
        this.path = path;
    }

    @Override
    public void prepare() {
        super.prepare();
        if (path == null || path.isEmpty()) {
            path = "dpi4g.properties";
        }
        Properties props = new Properties();
        try {
            props.load(this.getClass().getClassLoader().getResourceAsStream(path));
            connection = DriverManager.getConnection(props.getProperty("dburl"), props.getProperty("dbuser"), props.getProperty("dbpwd"));
        } catch (SQLException e) {
            e.printStackTrace();
            System.exit(-1);
        } catch (NullPointerException e) {
            try {
                e.printStackTrace();
                System.exit(-1);
            } catch (Exception e1) {
                e1.printStackTrace();
                System.exit(-1);
            }
        } catch (IOException e) {
            try {
                e.printStackTrace();
                System.exit(-1);
            } catch (Exception e1) {
                e1.printStackTrace();
                System.exit(-1);
            }
        }
    }

}
